package com.sugar.queryinfo.module.business.controller.admin.apitypeinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.math.BigDecimal;
import java.util.*;
import java.io.IOException;

import com.sugar.queryinfo.framework.common.pojo.PageParam;
import com.sugar.queryinfo.framework.common.pojo.PageResult;
import com.sugar.queryinfo.framework.common.pojo.CommonResult;
import com.sugar.queryinfo.framework.common.util.object.BeanUtils;
import static com.sugar.queryinfo.framework.common.pojo.CommonResult.success;

import com.sugar.queryinfo.framework.excel.core.util.ExcelUtils;

import com.sugar.queryinfo.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.queryinfo.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.queryinfo.module.business.controller.admin.apitypeinfo.vo.*;
import com.sugar.queryinfo.module.business.dal.dataobject.apitypeinfo.ApiTypeInfoDO;
import com.sugar.queryinfo.module.business.service.apitypeinfo.ApiTypeInfoService;

@Tag(name = "管理后台 - API类型")
@RestController
@RequestMapping("/business/api-type-info")
@Validated
public class ApiTypeInfoController {

    @Resource
    private ApiTypeInfoService apiTypeInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建API类型")
    @PreAuthorize("@ss.hasPermission('business:api-type-info:update')")
    public CommonResult<Long> createApiTypeInfo(@Valid @RequestBody ApiTypeInfoSaveReqVO createReqVO) {
        return success(apiTypeInfoService.createApiTypeInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新API类型")
    @PreAuthorize("@ss.hasPermission('business:api-type-info:update')")
    public CommonResult<Boolean> updateApiTypeInfo(@Valid @RequestBody ApiTypeInfoSaveReqVO updateReqVO) {
        apiTypeInfoService.updateApiTypeInfo(updateReqVO);
        return success(true);
    }

    @PutMapping("/update/status")
    @Operation(summary = "更新API类型状态")
    @PreAuthorize("@ss.hasPermission('business:api-type-info:update')")
    public CommonResult<Boolean> updateApiTypeInfoStatus(@Valid @RequestBody ApiTypeInfoUpdateStatusReqVO updateStatusReqVO) {
        apiTypeInfoService.updateApiTypeInfoStatus(updateStatusReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除API类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:api-type-info:delete')")
    public CommonResult<Boolean> deleteApiTypeInfo(@RequestParam("id") Long id) {
        apiTypeInfoService.deleteApiTypeInfo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除API类型")
                @PreAuthorize("@ss.hasPermission('business:api-type-info:delete')")
    public CommonResult<Boolean> deleteApiTypeInfoList(@RequestParam("ids") List<Long> ids) {
        apiTypeInfoService.deleteApiTypeInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得API类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:api-type-info:query')")
    public CommonResult<ApiTypeInfoRespVO> getApiTypeInfo(@RequestParam("id") Long id) {
        ApiTypeInfoDO apiTypeInfo = apiTypeInfoService.getApiTypeInfo(id);
        return success(BeanUtils.toBean(apiTypeInfo, ApiTypeInfoRespVO.class));
    }

    @GetMapping("/all")
    @Operation(summary = "获得所有API类型")
    public CommonResult<List<ApiTypeInfoSimpleRespVO>> getApiTypeInfos(@RequestParam(value = "type", required = false) Integer type) {
        return success(apiTypeInfoService.getApiTypeInfos(type));
    }

    @GetMapping("/get/{code}")
    @Operation(summary = "根据Code获得API类型")
    public CommonResult<ApiTypeInfoSimpleRespVO> getApiByCode(@PathVariable("code")String code) {
        return success(BeanUtils.toBean(apiTypeInfoService.getApiPriceByType(code), ApiTypeInfoSimpleRespVO.class));
    }

    @GetMapping("/get/znsh/price")
    @Operation(summary = "获取智能审核的价格")
    public CommonResult<BigDecimal> getApiZnshPrice() {
        return success(apiTypeInfoService.getApiZnshPrice());
    }

    @GetMapping("/page")
    @Operation(summary = "获得API类型分页")
    @PreAuthorize("@ss.hasPermission('business:api-type-info:query')")
    public CommonResult<PageResult<ApiTypeInfoRespVO>> getApiTypeInfoPage(@Valid ApiTypeInfoPageReqVO pageReqVO) {
        PageResult<ApiTypeInfoDO> pageResult = apiTypeInfoService.getApiTypeInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ApiTypeInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出API类型 Excel")
    @PreAuthorize("@ss.hasPermission('business:api-type-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportApiTypeInfoExcel(@Valid ApiTypeInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ApiTypeInfoDO> list = apiTypeInfoService.getApiTypeInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "API类型.xls", "数据", ApiTypeInfoRespVO.class,
                        BeanUtils.toBean(list, ApiTypeInfoRespVO.class));
    }

}